package me.zhengjie.kwService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.kwService.OccupyService;
import me.zhengjie.mapper.ExamMapper;
import me.zhengjie.mapper.MasterTearcherMapper;
import me.zhengjie.mapper.OccupyMapper;
import me.zhengjie.mapper.RoomMapper;
import me.zhengjie.pojo.dto.OccupyDto;
import me.zhengjie.pojo.entity.Exam;
import me.zhengjie.pojo.entity.MasterTearcher;
import me.zhengjie.pojo.entity.Occupy;
import me.zhengjie.pojo.entity.Room;
import me.zhengjie.pojo.vo.OccupyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Long
 * @date 2021/6/28 15:28
 */
@Service
public class OccupyServiceImpl extends ServiceImpl<OccupyMapper, Occupy> implements OccupyService {
    @Autowired
    private OccupyMapper occupyMapper;
    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private MasterTearcherMapper masterTearcherMapper;

    @Override
    public List<OccupyVo> findAll(OccupyDto dto) {
        QueryWrapper<Occupy> wrapper = new QueryWrapper<>();
        wrapper.ge(dto.getStartTime() != null, "start_time", dto.getStartTime())
                .le(dto.getEndTime() != null, "end_time", dto.getEndTime());
        List<Occupy> occupies = occupyMapper.selectList(wrapper);
        List<OccupyVo> list = new ArrayList<>();
        for (Occupy occupy : occupies) {
            OccupyVo occupyVo = new OccupyVo();
            BeanUtils.copyProperties(occupy, occupyVo);
            list.add(occupyVo);
        }
        for (OccupyVo occupyVo : list) {
            if (occupyVo.getExamId() != null) {
                Exam exam = examMapper.selectById(occupyVo.getExamId());
                occupyVo.setExam(exam);
            }
            if ("0".equals(occupyVo.getType())) {
                if(occupyVo.getTypeId()!=null){
                    Room room = roomMapper.selectById(occupyVo.getTypeId());
                    occupyVo.setRoom(room);
                }
            }
            if("1".equals(occupyVo.getType())){
                if(occupyVo.getTypeId()!=null){
                    MasterTearcher masterTearcher =
                            masterTearcherMapper.selectById(occupyVo.getTypeId());
                    occupyVo.setMasterTearcher(masterTearcher);
                }
            }
        }
        return list;
    }
}
